use GDBM_File;

%count = ();
%hash = ();
%hash_main = ();
while($line = <STDIN>)
{
	chomp($line);
	($main,$prob) = split(/\t/,$line);
	$hash_main{ $main } = $prob;
}
foreach $key (sort {$hash_main{$b} <=> $hash_main{$a}} keys(%hash_main))
{
	($src,$tgt) = split(/___/,$key);
	if($hash{ $src } eq "")
	{
		$hash{ $src } = $tgt;
		$count{ $src }= 1;
	}
	else
	{
		if($count{ $src } < $ARGV[1])
		{
			$hash{ $src } = $hash{ $src }."___".$tgt;
			$count{ $src }++;
		}
	}
}

tie(%LEX,GDBM_File,$ARGV[0],&GDBM_WRCREAT,0644);

foreach $key (keys %hash)
{
	$value = $hash{ $key };
	$value =~ s/\s+/_/g;
	print $key."  --> ".$value."\n"; 
	$LEX{$key} = $value;
}
